/**
 * 
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import systole.domain.clinicalInformation.Family;
import systole.domain.clinicalInformation.FamilyPatientBackground;
import systole.domain.persons.Patient;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;

import systole.persistence.brokersInterface.FamilyBackgroundBroker;
import systole.view.messages.ErrorMessages;

/**
 * @author jmj
 *
 */
public class FamilyBackgroundBrokerDB extends BrokerDB implements
        FamilyBackgroundBroker {

    /**
     *
     */
    public FamilyBackgroundBrokerDB() {
        super();
    }

    public void insert(FamilyPatientBackground familyPatientBackground) throws ExceptionDAO {
        try {
            this.logger.logDebug("saving patient family background");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(familyPatientBackground);
            this.logger.logDebug("saved patient family background");
        } catch (HibernateException e) {
            this.logger.logError("error on save patient family background, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    public void update(FamilyPatientBackground familyPatientBackground) throws ExceptionDAO {
        try {
            this.logger.logDebug("updating patient family background");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.update(familyPatientBackground);
            this.logger.logDebug("updated patient family background");
        } catch (HibernateException e) {
            this.logger.logError("error on update patient family background, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    public List<FamilyPatientBackground> getAllFamilyPatientBackground(Patient patient) throws ExceptionDAO {
        try {
            this.logger.logDebug("Getting all patient family background");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<FamilyPatientBackground> list = currentSession.createCriteria(FamilyPatientBackground.class).add(Restrictions.eq("patient", patient)).list();
            this.logger.logDebug("get patient family background");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get all patient family background, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los antecedentes familiares del paciente", e.fillInStackTrace());
        }
    }

    public boolean existFamilyPatientBackground(Family family, Patient patient) throws ExceptionDAO {
        try {
            this.logger.logDebug("exist patient family background");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<FamilyPatientBackground> list = currentSession.createCriteria(FamilyPatientBackground.class).add(Restrictions.and(Restrictions.eq("patient", patient), Restrictions.eq("family", family))).list();
            this.logger.logDebug("exist patient family background");
            return !list.isEmpty();
        } catch (HibernateException e) {
            this.logger.logError("error on exist medicine by patient, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo verificar si ya existe el medicamento para el paciente", e.fillInStackTrace());
        }
    }

    public void delete(FamilyPatientBackground familyPatientBackground) throws ExceptionDAO {
        try {
            this.logger.logDebug("deleting patient family background");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.delete(familyPatientBackground);
            this.logger.logDebug("deleted patient family background");
        } catch (HibernateException e) {
            this.logger.logError("error on delete patient family background, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo eliminar", e.fillInStackTrace());
        }
    }
}
